// pages/index/passage/passage.js
import Notify from '@vant/weapp/notify/notify';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    passage: {},
    comments: [],
    //写评论的遮掩层
    show: false,
    //评论内容
    message: '',
    //父级评论id
    pid: '',
    //父级评论userid
    puser: '',
    loadModel:true
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad(options) {
    //获取文章，以及评论
    let id = options.id
    app.request({
      url: app.globalData.apiUrl + '/passage/detail/' + id,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          passage: res.data.data.passage,
          comments: res.data.data.comment
        })
        let temp=this.data.comments;
        for (var i in temp) {
          if (temp[i].pid != null || '') {
            let temp_index=this.getArrayItemIndex(temp,temp[i].pid)
            temp[i].pbody=temp[temp_index].body
          }
        }
        this.setData({
          comments:temp,
          loadModel:false
				})
				console.log(this.data.comments)
      } else {
        this.setData({
          loadModel: false
        })
        Notify('服务错误，稍后重试');
      }
    })
  },
  //获取数组中某个元素属性的下标
  getArrayItemIndex(arr, id) {
    var index = -1;
    if (typeof Array.prototype.findIndex === 'function') {
      index = arr.findIndex(function (item) {
        return item.id === id;
      });
    } else {
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].id === id) {
          index = i;
          break;
        }
      }
    }
    return index;
  },
  //操作写评论的弹出层
  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false,
      message: '',
      pid: '',
      puser: ''
    });
  },

  //写子级评论弹出层，需要接收父级评论参数
  showPopup2(e) {
    console.log(e)
    this.setData({
      show: true,
      pid: e.target.dataset.pid,
      puser: e.target.dataset.puser
    });
  },
  //提交评论
  commit() {
    let comment = JSON.stringify({
      type: 0,
      textId: this.data.passage.id,
      body: this.data.message,
      puser: this.data.puser,
      pid: this.data.pid,
      createTime: Date.parse(new Date())
    })
    app.request({
      url: app.globalData.apiUrl + '/comment' ,
			method: 'POST',
			data:{
				comment:comment
			}
    }).then(res => {
      this.onClose()
      if (res.statusCode == 200) {
        app.request({
          url: app.globalData.apiUrl + '/comment/0/' + this.data.passage.id,
          method: 'GET',
        }).then(res => {
          if (res.statusCode == 200) {
            let temp=res.data.data;
            for (var i in temp) {
              if (temp[i].pid != null || '') {
                let temp_index=this.getArrayItemIndex(temp,temp[i].pid)
                temp[i].pbody=temp[temp_index].body
              }
						}
						console.log(temp)
            this.setData({
							comments:temp,
						})
						console.log(this.data.comments)
          } else {
						Notify(
							 '服务错误，稍后重试'
						)
          }
        })
      } else {
        loadModel: false
        Notify('服务错误，稍后重试');
      }
    })
  },
  gotoShow(e){
    console.log(e)
		wx.navigateTo({
			url: '/pages/show/show?id='+e.currentTarget.dataset.id,
		})
	}
})